/**
 *点击路由
 */
'use strict';

const conn = require('../../libs/DB/Conn');
const _ = require('underscore');


module.exports = async function (params) {
    let formValues = params;
    let routeId = formValues.routeId;
    let path = formValues.path;
    let menuId = formValues.menuId;
    let isFirst = formValues.isFirst;
    let menuPath = formValues.menuPath;
    let redirect = `${menuPath}/${path}`;//跳转路径由菜单路径+页面路径生成
    let firstUpperCase = path.slice(0, 1).toUpperCase();
    let restStr = path.slice(1);
    let url = `${firstUpperCase}${restStr}`;//页面文件名将页面路径首字母转换为大写直接匹配对应调用的Vue文件名
    let checkSql = 'SELECT * FROM `router` WHERE menu_id = ? ';
    let checkResult = await conn(checkSql, [menuId]);
    if (isFirst) {//判断该页面下是否存已经存在了一个一级页面如果已经存在则不允许添加！
        let filterFirst = _.filter(checkResult, {is_first: 1});
        if (filterFirst.length && filterFirst[0].router_id != routeId) return {
            success: false,
            message: '一个菜单下只允许一个一级页面'
        };
    }
    let checkPath = _.filter(checkResult, {path: path});//判断该path是否重复！
    if (checkPath.length && checkPath[0].router_id != routeId) return {success: false, message: 'path重复'};
    let updateMenuObj = {
        path: path,
        redirect: redirect,
        url: url,
        is_first: isFirst
    };
    let updateSql = `UPDATE router SET ? WHERE router_id = ?`;
    await conn(updateSql, [updateMenuObj, routeId]);
    return {success: true};
};
